package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.GuseBook;
import cn.jdy.plugins.service.CompanyService;
import cn.jdy.plugins.service.GuseBookService;

/**
 * 留言控制类
 * @author wuzhiliang
 *
 */
@Controller
@RequestMapping("/leaveword")
public class LeavewordCtrl {

	@Autowired
	private GuseBookService guseBookService;
	@Autowired
	private CompanyService companyService;
	
	/**
	 * 留言管理初始化
	 * @return
	 */
	@RequestMapping("/init")
	public ModelAndView init(){
		ModelAndView mv = new ModelAndView("/company/leavewordList.jsp");
		mv.addObject("companys", companyService.findCompanyList());
		return mv;
	}
	
	/**
	 * 查询留言列表
	 * @param bo
	 * 
	 */
	@RequestMapping("/query")
	@ResponseBody
	public Map<String,Object> query(GuseBook bo){
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		try {
			List<GuseBook> list  = null;
			
			int Count = guseBookService.getCount(bo);
					
			 list  = guseBookService.query(bo);
			jsonMap.put("status", 200);
			
			jsonMap.put("dataList", list);
			jsonMap.put("iTotalRecords", Count);
			jsonMap.put("iTotalDisplayRecords", Count);
			
		} catch (Exception e) {
			jsonMap.put("status", 500);
			BaseLog.e(this.getClass(), "查询留言列表出错", e);
		}
		return jsonMap;
	}
	
	/**
	 * 审核留言信息
	 * @param id
	 * @param status
	 * @param advice
	 * 
	 */
	@RequestMapping("/examine")
	@ResponseBody
	public Map<String,Object> examine(String id ,String status ,String advice){
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		try {
			GuseBook bo = guseBookService.find(id);
			bo.setStatus(status);
			bo.setAdvice(advice);
			guseBookService.update(bo);
			jsonMap.put("statusCode", 200);
		} catch (Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(this.getClass(), "审核留言信息出错", e);
		}
		
		return jsonMap;
	}

	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable String id) {
		ModelAndView mv = new ModelAndView("/company/leaveMsgDetail.jsp");
		mv.addObject("data", this.guseBookService.find(id));
		return mv;
	}
	/**
	 * 编辑
	 * @param entity
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(GuseBook entity) {
		ModelAndView mv = new ModelAndView("redirect:/leaveword/init");
		guseBookService.update(entity);
		return mv;
	}

	/**
	 * 删除留言
	 * @param id
	 */
	@RequestMapping("/del")
	@ResponseBody
	public Map<String, Object> del(String id) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			guseBookService.delete(id);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(this.getClass(), "删除消息异常", e);
		}
		return jsonMap;
	}
}
